package com.beiding.workordersystem.common.helper;

import com.beiding.workordersystem.common.helper.IdCreator;
import com.beiding.workordersystem.common.standard.E;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;

@Component
public class EntityHelper {

    @Autowired
    private IdCreator idCreator;

    //返回是否填充成功
    public boolean fillId(Object o, String idName) {
        try {
            Field declaredField = o.getClass().getDeclaredField(idName);
            declaredField.setAccessible(true);
            Object st = declaredField.get(o);
            if (st == null || "".equals(st)) {
                declaredField.set(o, idCreator.create());
                return true;
            }
            return false;
        } catch (NoSuchFieldException | IllegalAccessException e) {

            throw new RuntimeException(e);
        }

    }

    public boolean fillId(Object o) {
        return fillId(o, "id");
    }


}
